package com.demo376_摆动序列;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

//  序列可以是里面的子序列, 也就是可以是删除掉一些元素后的序列
public class Solution_去重 {
    public static void main(String[] args) {
        int[] ints = {1,2,3,4,5,6,7,8,9};
        int i = wiggleMaxLength(ints);
        System.out.println(i);
    }
    public static int wiggleMaxLength(int[] nums) {
        //1.把连续的为正的元素去重
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            list.add(nums[i]);
        }
        if (nums.length==1){
            return 1;
        }
        if(nums.length==2){
            if (nums[0]!=nums[1]){
                return 2;
            }else {
                return 1;
            }
        }
        for (int i = 2; i < list.size(); i++) {
            if ((list.get(i)-list.get(i-1))*(list.get(i-1)-list.get(i-2))>0){
                list.remove(i-1);
                i--;
            }else if (list.get(i)-list.get(i-1)==0){
                list.remove(i-1);
                i--;
            }
        }
        int[] ints = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ints[i]=list.get(i);
        }

        int max = getMax(ints);

        return max;


    }
    public static int getMax(int[] nums) {
        //思路: 设计两个指针,一前一后, 如果不同则记录


        if (nums.length <=2) {
            return nums.length;
        }
        int flag = 0;
        if (nums[1] > nums[0]) {
            flag = 1;// 为正为true
        }
        if (nums[1] < nums[0]) {
            flag = -1;
        }
        int max = 0;
        int startIndex = 0;
        for (int i = 2; i < nums.length; i++) {
            // 大于
            if (nums[i] > nums[i - 1]) {
                // 如果上一个是>0
                if (flag == 1) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }else if (flag == -1) {// 如果上一个是<0
                    int len=i-startIndex+1;
                    max = Math.max(max, len);
                    flag = 1;
                }else if (flag == 0) {// 如果等于0
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }
            }
            // 小于
            if (nums[i] < nums[i - 1]) {
                //如果上一个是大于0
                if (flag == 1) {
                    int len=i-startIndex+1;
                    max = Math.max(max, len);
                    flag = -1;
                } else if (flag == -1) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                } else if (flag == 0) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }
            }

            if (nums[i] == nums[i - 1]) {
                int len = i - startIndex;
                startIndex = i;
                max = Math.max(max, len);
            }



        }
        return max;
    }
}
